package com.aisb.manage;

import com.aisb.core.Result;
import com.aisb.core.log.LogOper;
import com.aisb.core.log.LogVisit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Description：
 * @Author wucm
 * @Date 2017/12/28
 */
@Controller
@RequestMapping("/manage/auth")
public class AuthController {
    private static Logger logger = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    AuthService authService;

    @RequestMapping(value = "/index")
    @RequiresPermissions("/manage/auth/index")
    @LogVisit("功能权限管理列表")
    public String index(Model model) {
        return "manage/auth";
    }

    @RequestMapping(value = "/scope/index")
    @RequiresPermissions("/manage/auth/scope/index")
    @LogVisit("数据权限管理列表")
    public String indexScope(Model model) {
        return "manage/scope";
    }

    @RequestMapping(value = "/allurf")
    @ResponseBody
    public Result allUserRoleFunc(@RequestParam Map param) {
        List uList = authService.userList();
        List rList = authService.roleList();
        List fList = authService.funcList();
        List aList = authService.areaList();

        for (Object m : aList) {
            ((Map) m).put("nocheck", "true");
        }

        uList.addAll(authService.dealAreaTree(uList, aList));
        rList.addAll(authService.dealAreaTree(rList, aList));

        Map r = new HashMap();
        r.put("users", uList);
        r.put("roles", rList);
        r.put("funcs", fList);

        return new Result(true, r);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    @LogOper("功能权限管理，新增功能权限")
    public Result authAdd(@RequestParam Map param) {
        String users = (String) param.get("users");
        String roles = (String) param.get("roles");
        String funcs = (String) param.get("funcs");

        List<String> userIds = new ArrayList<String>();
        List<String> roleIds = new ArrayList<String>();
        List<String> funcIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != roles && roles.length() > 0) {
            String[] j = roles.split(";");
            roleIds = Arrays.asList(j);
        }
        if (null != funcs && funcs.length() > 0) {
            String[] f = funcs.split(";");
            funcIds = Arrays.asList(f);
        }

        int result = authService.authAdd(userIds, roleIds, funcIds);

        return new Result(true, result);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    @LogOper("功能权限管理，删除功能权限")
    public Result authDelete(@RequestParam Map param) {
        String users = (String) param.get("users");
        String roles = (String) param.get("roles");
        String funcs = (String) param.get("funcs");

        List<String> userIds = new ArrayList<String>();
        List<String> roleIds = new ArrayList<String>();
        List<String> funcIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != roles && roles.length() > 0) {
            String[] j = roles.split(";");
            roleIds = Arrays.asList(j);
        }
        if (null != funcs && funcs.length() > 0) {
            String[] f = funcs.split(";");
            funcIds = Arrays.asList(f);
        }

        int result = authService.authDelete(userIds, roleIds, funcIds);

        return new Result(true, result);
    }

    @RequestMapping(value = "/add/userrole")
    @ResponseBody
    @LogOper("用户管理，角色配置")
    public Result authAddUserRole(@RequestParam Map param) {
        String users = (String) param.get("users");
        String roles = (String) param.get("roles");

        List<String> userIds = new ArrayList<String>();
        List<String> roleIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != roles && roles.length() > 0) {
            String[] j = roles.split(";");
            roleIds = Arrays.asList(j);
        }

        int result = authService.authAddRoleUser(roleIds, userIds, true);

        return new Result(true, result);
    }

    @RequestMapping(value = "/add/rolefunc")
    @ResponseBody
    @LogOper("角色管理，功能配置")
    public Result authAddRoleFunc(@RequestParam Map param) {
        String funcs = (String) param.get("funcs");
        String roles = (String) param.get("roles");

        List<String> funcIds = new ArrayList<String>();
        List<String> roleIds = new ArrayList<String>();


        if (null != funcs && funcs.length() > 0) {
            String[] u = funcs.split(";");
            funcIds = Arrays.asList(u);
        }
        if (null != roles && roles.length() > 0) {
            String[] j = roles.split(";");
            roleIds = Arrays.asList(j);
        }

        int result = authService.authAddRoleFunc(roleIds, funcIds, true);

        return new Result(true, result);
    }

    // 数据权限============================================================================================

    @RequestMapping(value = "/scope/allua")
    @ResponseBody
    public Result allUserArea(@RequestParam Map param) {
        List uList = authService.userList();
        List aList = authService.areaList();

        uList.addAll(authService.dealAreaTree(uList, aList));

        Map r = new HashMap();
        r.put("users", uList);
        r.put("areas", aList);

        return new Result(true, r);
    }

    @RequestMapping(value = "/scope/add")
    @ResponseBody
    @LogOper("新增数据权限")
    public Result scopeAdd(@RequestParam Map param) {
        String users = (String) param.get("users");
        String areas = (String) param.get("areas");

        List<String> userIds = new ArrayList<String>();
        List<String> areaIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != areas && areas.length() > 0) {
            String[] j = areas.split(";");
            areaIds = Arrays.asList(j);
        }

        int result = authService.authScopeAdd(userIds, areaIds);

        return new Result(true, result);
    }

    @RequestMapping(value = "/scope/delete")
    @ResponseBody
    @LogOper("删除数据权限")
    public Result scopeDelete(@RequestParam Map param) {
        String users = (String) param.get("users");
        String areas = (String) param.get("areas");

        List<String> userIds = new ArrayList<String>();
        List<String> areaIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != areas && areas.length() > 0) {
            String[] j = areas.split(";");
            areaIds = Arrays.asList(j);
        }

        int result = authService.authScopeDelete(userIds, areaIds);

        return new Result(true, result);
    }


    @RequestMapping(value = "/scopeexclude/add")
    @ResponseBody
    @LogOper("新增数据排除权限")
    public Result scopeExcludeAdd(@RequestParam Map param) {
        String users = (String) param.get("users");
        String areas = (String) param.get("areas");

        List<String> userIds = new ArrayList<String>();
        List<String> areaIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != areas && areas.length() > 0) {
            String[] j = areas.split(";");
            areaIds = Arrays.asList(j);
        }

        int result = authService.authScopeExcludeAdd(userIds, areaIds);

        return new Result(true, result);
    }

    @RequestMapping(value = "/scopeexclude/delete")
    @ResponseBody
    @LogOper("删除数据排除权限")
    public Result scopeExcludeDelete(@RequestParam Map param) {
        String users = (String) param.get("users");
        String areas = (String) param.get("areas");

        List<String> userIds = new ArrayList<String>();
        List<String> areaIds = new ArrayList<String>();


        if (null != users && users.length() > 0) {
            String[] u = users.split(";");
            userIds = Arrays.asList(u);
        }
        if (null != areas && areas.length() > 0) {
            String[] j = areas.split(";");
            areaIds = Arrays.asList(j);
        }

        int result = authService.authScopeExcludeDelete(userIds, areaIds);

        return new Result(true, result);
    }
}
